    <?php
    include('db_query_user.php'); 
    if(($_GET['act']!='edit' && $_GET['company_id'] <=0) && $_GET['act']!='add') {
    ?>
    <form action="" method="post" id="frmCompany"  name="frmCompany"> 	 	 	 	 	
        <div id="table-company">
            <div class="tab-header">
                Select: <select id="status" name="status">                            
                            <option value="0" <?php print $_GET['selected']=='0'?'selected=selected':'';?>>Pending&nbsp;</option>
                            <option value="1" <?php print $_GET['selected']=='1'?'selected=selected':'';?>>Approved&nbsp;</option>                            
                            <option value="-1" <?php print $_GET['selected']=='-1'?'selected=selected':'';?>>Deleted</option>
                            <option value="3" <?php print (!isset($_GET['selected']) || $_GET['selected'] == '' || $_GET['selected']=='3')?'selected=selected':'';?>>-All-</option>
                          </select>                      
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="text" size="40" id="txtSearchCompany" name="txtSearchCompany" value="" />
                <input  class="button" type="submit" value="Search" id="btnSearch" name="btnSearch" />
            </div>
            <div class="header row">
                <div class="col1 col"><input type="checkbox" id="chk_all" name="chk_all" /></div>
                <div class="col2 col">#</div>
                <div class="col3 col">
				<?php
				
				$arr_opt = array();
				$arr_opt = array_merge($_GET);
				$sql_order = '';
				
				if(!isset($_GET['sort_by']) || $_GET['sort_by'] == '' ) {
					$arr_opt['sort_by'] = 're_order';
					$arr_opt['sort_dir'] = 'desc';	
				} 
				
				//build sql
				$sql_order .= ' ORDER BY ' . $arr_opt['sort_by'] . ' ' . $arr_opt['sort_dir'];
				
				unset($arr_opt['sort_by']);
				if($arr_opt['sort_dir'] == 'asc') {
					$arr_opt['sort_dir'] = 'desc';
				} else {
					$arr_opt['sort_dir'] = 'asc';
				}
				
				$com_sort_uri = '?';
				
				foreach(array_keys($arr_opt) as $opt) {
					$com_sort_uri .= $opt . '=' . $arr_opt[$opt] . '&';
				}
				
				$com_sort_uri .= 'sort_by=name';
				
				?>
				<a href="<?php echo $com_sort_uri; ?>">Company Name</a>
				</div>
                <div class="col4 col">Company Code</div>
                <div class="col5 col">Status</div>
                <div class="col6 col">Details</div>
                <!--<div class="col7 col">Delete</div>--> 
            </div>
            <?php
            $where = '';
            
            $first = 0;
            $size = 15;
            $page = (!is_numeric($_GET['pg']))?1:$_GET['pg'];

			
            if(is_numeric($_GET['selected']) && $_GET['selected'] != 3) {
				$where = "WHERE com.approved = ".$_GET['selected'];
			} else {
				$where = "WHERE com.approved in (0, 1, -1)"; // Incase display ALL
			}
			/*
            $sql = "SELECT com.*, u.user_status FROM ".$wpdb->prefix."company com
                    LEFT JOIN ".$wpdb->prefix."users u ON u.ID = com.user_id
                    $where"; */
			$sql = "SELECT CASE approved WHEN 0 THEN 1 WHEN 1 THEN 0 ELSE -1 END AS re_order, com.*, u.user_status FROM ".$wpdb->prefix."company com
                    LEFT JOIN ".$wpdb->prefix."users u ON u.ID = com.user_id
                    $where";
            $sql_count = "SELECT count(*) as total FROM (".$sql.") as tblCount";            
            $db_count = $wpdb->get_row($sql_count);
            $total_rec = $db_count->total;
            $total_page = (int)($db_count->total/$size);
            if($total_page<1) 
                $total_page = 1;
            else 
                if($total_page*$size < $db_count->total ) $total_page++;
                
            $current_page = ($_GET['pg'] >0)? $_GET['pg'] : 1;
            $from_row = $size*($current_page-1) + 1;
            $to_row  = $size*($current_page-1) + $size;
            if($to_row > $total_rec) $to_row  = $total_rec;
                   
            // $sql .= " ORDER BY id DESC LIMIT ".($page - 1)*$size.", $size";
			$sql .= $sql_order . " LIMIT ".($page - 1)*$size.", $size";
//            print_r($sql);
            $db = $wpdb->get_results($sql);
            //user statsu 0: new -> user active -> 1, admin active -> 2
            $i = 1;
            foreach($db as $company) { 
                
                $status = '<font color="#bd0000">Waiting for Approval</font>';
                if($company->approved == 1) $status = 'Approved';
                if($company->approved == -1) $status = 'Deleted';
                
                ?>
            <div class="row">
                <div class="col1 col"><input class="chk_item" type="checkbox" id="chk_item<?php print $company->id; ?>" name="chk_item[]" value="<?php print $company->id; ?>" /></div>
                <div class="col2 col"><?php print $i++;?></div>
                <div class="col3 col"><?php print $company->name;?></div>
                <div class="col4 col"><?php print $company->company_code;?></div>     
                <div class="col5 col">
                     <?php print $status; ?>
                </div>           
                <div class="col6 col"><a href="?page=accm&tabmenu=company&act=edit&company_id=<?php print $company->id; ?>#company">View</a></div>
                <!--<div class="col7 col"><a href="?page=accm&tabmenu=company&act=del&user_id=<?php print $company->user_id; ?>#company">Delete</a></div>-->                
            </div>
             <?php } ?>
        </div>     
        <div class="footer">
            <div class="paging">
                <div class="left"><strong>Displaying <?php print $from_row?> to <?php print $to_row;?> of <?php print $db_count->total;?></strong></div>
                <div class="right"><strong>Go to</strong>:
                    <?php for($i=1; $i<= $total_page; $i++) { 
                            if($page==$i) {
                        ?> 
                         <a class="curent-page" href="#"><?php print $i;?></a>
                        <?php } else { ?>
                        <a href="?page=accm&tabmenu=company&selected=<?php print $_GET['selected'];?>&pg=<?php print $i;?>"><?php print $i;?></a>
                        <?php }                        
                         } //for ?>
                </div>
            </div>
            <input type="button" value="Add Company" id="btnAddCompany" name="btnAddCompany" class="button">
            <input type="submit" value="Approve" id="btnApprove" name="btnApprove" class="button">          
            <input type="submit" value="Delete" id="btnDelete" name="btnDelete" class="button">
        </div>
    </form>        
    <?php
       } else  {
    ?>         
    <div class="wrap" id="register_user" style="width: <?php echo ($is_admin)?'700px':'auto'; ?>;">        
        <?php 
            $company_id =  (is_numeric($_GET['company_id']))?$_GET['company_id']: 0;
            //pagin            
            
            $sql = "SELECT com.*, con.first_name, con.last_name, con.address1 as con_address1, con.address2 as con_address2,
                        con.city_state_zip as con_city_state_zip, con.phone as con_phone, con.fax,  con.questions, con.email as con_email, u.user_login as user_email                        
                    FROM ".$wpdb->prefix."company com 
                    LEFT JOIN ".$wpdb->prefix."contact con ON com.user_id = con.user_id
                    INNER JOIN ".$wpdb->prefix."users u ON u.ID = com.user_id 
                    WHERE com.id = ".$company_id;                   
           
            $row = $wpdb->get_row($sql);
            
            $com_Name           = ($_GET['company_id']>0)?$row->name:$_POST['CompanyName'];
            $com_Address        = ($_GET['company_id']>0)?$row->address1:$_POST['Address'];
            $com_Address2       = ($_GET['company_id']>0)?$row->address2:$_POST['CompanyAddress2'];
            $com_CityStateZip   = ($_GET['company_id']>0)?$row->city_state_zip:$_POST['CompanyCityStateZip'];
            $com_Phone          = ($_GET['company_id']>0)?$row->phone:$_POST['CompanyPhone'];
            $com_Website        = ($_GET['company_id']>0)?$row->website:$_POST['Website'];
            $com_BusinessDescription        = ($_GET['company_id']>0)?$row->business_description:$_POST['BusinessDescription'];
            $com_OnlineAdvertising          = ($_GET['company_id']>0)?$row->online_advertising:$_POST['OnlineAdvertising'];
            $com_CitywidePlannerWorkbook    = ($_GET['company_id']>0)?$row->citywide_planner:$_POST['CitywidePlannerWorkbook'];
            $com_VisitSanDiegoGuide         = ($_GET['company_id']>0)?$row->visit_sdg:$_POST['VisitSanDiegoGuide'];
            $com_LobbyAdvertising           = ($_GET['company_id']>0)?$row->lobby_advertising:$_POST['LobbyAdvertising'];
            $com_Code                       = ($_GET['company_id']>0)?$row->company_code:$_POST['CompanyCode'];
            
            if(empty($com_Code)) $com_Code = generate_random_password(6, 2);
            //contact 
            $con_FirstName = ($_GET['company_id']>0)?$row->first_name:$_POST['ContactFirstName'];
            $con_LastName  = ($_GET['company_id']>0)?$row->last_name:$_POST['ContactLastName'];
            $con_Address1  = ($_GET['company_id']>0)?$row->con_address1:$_POST['Address1'];
            $con_Address2  = ($_GET['company_id']>0)?$row->con_address2:$_POST['Address2'];
            $con_CityStateZip = ($_GET['company_id']>0)?$row->con_city_state_zip:$_POST['CityStateZip'];
            $con_Phone        = ($_GET['company_id']>0)?$row->con_phone:$_POST['ContactPhone'];
            $con_Fax          = ($_GET['company_id']>0)?$row->fax:$_POST['Fax'];
            $con_Email        = ($_GET['company_id']>0)?$row->con_email:$_POST['Email'];
            $con_Comments        = ($_GET['company_id']>0)?$row->questions:$_POST['Comments']; 
            $user_email = ($_GET['company_id']>0)?$row->user_email:$_POST['Email'];   
            $user_id = $row->user_id;
            $user_info = get_userdata($row->user_id);          
      ?>
        <form enctype="multipart/form-data" action="" method="post" name="frmRegister" id="frmRegister">	
        <table width="100%" cellspacing="5" cellpadding="0" border="0">
        <tbody>        
        <tr valign="top" width="100%"><td align="right" colspan="2"><i><font color="#bd0000">*</font> Indicates Required Information</i></td></tr>        
		<tr><td colspan="2" class="formheader1">(New) Company Information</td></tr>
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Company Name:</label></td><td align="left" style="width:72%;">        	
                	<input type="text" class="required" value="<?php print $com_Name;?>" name="CompanyName" id="CompanyName">                
            </td></tr>        
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Address 1:</label></td><td align="left" style="width:72%;">        	
                	<input type="text" class="required" value="<?php print $com_Address;?>" name="Address" id="Address">                
        	</td></tr>            
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label>Address 2:</label></td><td align="left" style="width:72%;">        	
                	<input type="text" value="<?php print $com_Address2;?>" name="CompanyAddress2" id="CompanyAddress2">                
        	</td></tr>        
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> City, State, Zip:</label></td><td align="left" style="width:72%;">        	
                	<input type="text" class="required" value="<?php print $com_CityStateZip;?>" name="CompanyCityStateZip" id="CompanyCityStateZip">                
        	</td></tr>		
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Phone:</label></td><td align="left" style="width:72%;">
        	                	<input type="text" class="required" value="<?php print $com_Phone;?>" name="CompanyPhone" id="CompanyPhone">                
            </td></tr>
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label>Website:</label></td><td align="left" style="width:72%;">        	
                	<input type="text" value="<?php print $com_Website;?>" name="Website" id="Website">
        	</td></tr>
        <tr valign="top" class="inputfield"><td align="right" style="vertical-align:top;width:28%;"><label>
        Business Description:</label></td><td align="left" style="width:72%;">    
            <textarea id="BusinessDescription" name="BusinessDescription" rows="10"><?php print $com_BusinessDescription; ?></textarea>  
                   	
       	        	
        </td></tr>        
        <tr valign="top" class="inputfield">
        	<td align="right" style="width:28%;"></td>
        	<td align="left" style="width:72%;vertical-align:top;">
            	The company interested in the following advertising opportunities (check all that apply):<br>            	
            		<input type="checkbox" name="OnlineAdvertising" value="1" <?php print ($com_OnlineAdvertising==1?'checked=true':'');?>>&nbsp;Online Advertising<br>                
            		<input type="checkbox" name="CitywidePlannerWorkbook" value="1" <?php print ($com_CitywidePlannerWorkbook==1?'checked=true':'');?>>&nbsp;Citywide Planner Workbook<br>                
            		<input type="checkbox" name="VisitSanDiegoGuide" value="1" <?php print ($com_VisitSanDiegoGuide==1?'checked=true':'');?>>&nbsp;Visit San Diego Guide<br>                
            		<input type="checkbox" name="LobbyAdvertising" value="1" <?php print ($com_LobbyAdvertising==1?'checked=true':'');?>>&nbsp;Lobby Advertising
            </td>
        </tr>
        <input type="hidden" value="<?php print $com_Code;?>" name="CompanyCode" id="CompanyCode">
        <tr><td colspan="2" class="formheader1">Contact Information</td></tr>
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Contact First Name:</label></td><td align="left" style="width:72%;"><input type="text" class="required" value="<?php print $con_FirstName;?>" name="ContactFirstName" id="ContactFirstName"></td></tr>        
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Contact Last Name:</label></td><td align="left" style="width:72%;"><input type="text" class="required" value="<?php print $con_LastName;?>" name="ContactLastName" id="ContactLastName"></td></tr>        
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label>Address 1:</label></td><td align="left" style="width:72%;"><input type="text" value="<?php print $con_Address1;?>" name="Address1" id="Address1"></td></tr>
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label>Address 2:</label></td><td align="left" style="width:72%;"><input type="text" value="<?php print $con_Address2;?>" name="Address2" id="Address2"></td></tr>
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label>City, State, Zip code:</label></td><td align="left" style="width:72%;"><input type="text" value="<?php print $con_CityStateZip;?>" name="CityStateZip" id="CityStateZip"></td></tr>        
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label>Phone:</label></td><td align="left" style="width:72%;"><input type="text" value="<?php print $con_Phone;?>" name="ContactPhone" id="ContactPhone"></td></tr>
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label>Fax:</label></td><td align="left" style="width:72%;"><input type="text" value="<?php print $con_Fax;?>" name="Fax" id="Fax"></td></tr>
        <tr valign="top" class="inputfield"><td align="right" style="width:28%;"><label><font color="#bd0000">*</font> Email: </label></td><td align="left" style="width:72%;"><input type="text" class="required" value="<?php print $con_Email;?>" name="Email" id="Email" autocomplete="off"></td></tr>      
        <!--<tr valign="top" class="inputfield"><td align="right" style="vertical-align:top;width:28%;"><label>Questions or Comments:</label></td><td align="left" style="width:72%;"><textarea cols="50" rows="10" name="Comments" id="Comments"><?php print $con_Comments;?></textarea></td></tr>-->
        <tr><td colspan="2" class="formheader1">Account Information</td></tr>
        <tr valign="top" class="inputfield">
        	<td align="right"></td>
        	<td align="left">
            	* Password:	(System Generated, Password will be emailed when added)
            </td>
        </tr>         
        <tr valign="top" width="100%"><td valign="top" align="right" colspan="2" style="padding-top: 20px;">
            <input id="user_email" name="user_email" value="<?php print $user_email; ?>" type="hidden">
            <input id="hdUserID" name="hdUserID" value="<?php print $user_id; ?>" type="hidden">
            <a class="button" href="?page=accm&tabmenu=company" style="padding: 6px 20px !important;">Cancel</a>
			<input type="button" id="ResetRegistration" name="ResetRegistration" value="Reset"  class="button">&nbsp;&nbsp;<input type="submit" id="SubmitRegistration" name="SubmitRegistration" value="Submit"  class="button">
        </td></tr>        
    </tbody></table>
    <strong>Note:</strong> <br />
    If you have deleted an item accidentally, you can restore it through database. Please contact your immediate web administrator to restore important data. <br />     
    Kindly manage your items properly.
    </form>
    </div>
    <?php } ?>
    <script type="text/javascript">
        (function($){
            $(document).ready(function() {
                 $('#status').change(function(){                        
                        window.location = "?page=accm&tabmenu=company&selected=" + $(this).val();
                 });  
                 $('#btnAddCompany').click(function(){                        
                        window.location = "?page=accm&tabmenu=company&act=add#company";
                 });
            });    
             
        })(jQuery);
     </script>
